//
//  LoginCityStarViewViewController.m
//  cityStar
//
//  Created by Hancong on 14/12/24.
//  Copyright (c) 2014年 tiny. All rights reserved.
//

#import "LoginCityStarViewViewController.h"
#import "RegisterCityStarViewController.h"
#import "NSString+MD5.h"
#import "HTDefine.h"


@interface LoginCityStarViewViewController ()<UITextFieldDelegate>

@end

@implementation LoginCityStarViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"登录";
    self.btnLogin.layer.cornerRadius = 4.0;
    self.btnLogin.clipsToBounds = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
//    [self addBackButton];
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 90, 30)];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setFrame:CGRectMake(-30, 0, 90, 30)];
    //    [leftButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [leftButton setTitle:@"返回" forState:UIControlStateNormal];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [leftButton setTitleColor:[UIColor colorWithWhite:0.200 alpha:1.000] forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor colorWithRed:0.991 green:0.532 blue:0.303 alpha:1.000] forState:UIControlStateHighlighted];
    [leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:@"back_touch"] forState:UIControlStateHighlighted];
    
    //    UIImageView *imagebackView = [[UIImageView alloc]initWithFrame:CGRectMake(-10, 0, 30, 30)];
    //    imagebackView.image = [UIImage imageNamed:@"back"];
    //    [view addSubview:imagebackView];
    [view addSubview:leftButton];
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    self.navigationItem.leftBarButtonItem = barButtonItem;
    
    __weak typeof(self)weakSelf = self;
    [self addRightButtonWithTitle:@"注册" actionBlock:^{
        DLog(@"注册");
        RegisterCityStarViewController *registerCityStarVC = [[RegisterCityStarViewController alloc]init];
        [weakSelf.navigationController pushViewController:registerCityStarVC animated:YES];
        
    }];
}
-(void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [self.view endEditing:YES];
}

-(IBAction)login_Touch:(id)sender
{
    NSString *strgetcode = [NSString stringWithFormat:@"%@%@",KInterFace_getcode,self.fieldUserName.text];
    [HttpRequestManager getMethod:strgetcode parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        DLog(@"%@",response);
        if (success) {
            NSString *addStrMD5 = [NSString stringWithFormat:@"%@%@%@",self.fieldPassWord.text,@"%",self.fieldUserName.text];
            DLog(@"%@",[addStrMD5 MD5Hash]);
            NSString *strMD5 = [NSString stringWithFormat:@"%@%@",[addStrMD5 MD5Hash],@")]}"];
            NSString *md5Code = response[@"value"][@"code"];
            NSString *lastMD5 = [NSString stringWithFormat:@"%@+%@",[strMD5 MD5Hash],md5Code];
            NSString *laststringed = [lastMD5 MD5Hash];
            
            NSDictionary *parameters = @{@"user":self.fieldUserName.text,
                                         @"password":[laststringed lowercaseString]};
            [HttpRequestManager postMethod:KInterFace_login parameters:parameters completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
                if (success) {
                    [SVProgressHUD showSuccessWithStatus:@"登录成功"];
                    [HttpRequestManager getMethod:KInterFace_getPersonalDetail parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
                        if (success) {
                            DLog(@"%@",response);
                            PersonalDetail *personalDetail = [PersonalDetail objectFromDictionary:response[@"value"]];
                            NSData *data = [NSKeyedArchiver archivedDataWithRootObject:personalDetail];
                            [KStandUserDefaults setObject:data forKey:KUserDefaultKey_PersonalDetail];
                            [KStandUserDefaults synchronize];
                            [[NSNotificationCenter defaultCenter]postNotificationName:@"LoginCityStarViewSuccess" object:self userInfo:nil];
                            [self dismissViewControllerAnimated:YES completion:nil];
                        }else{
                            [SVProgressHUD showErrorWithStatus:errMsg];
                        }
                    }];
                }else{
                    [SVProgressHUD showErrorWithStatus:errMsg];
                }
            }];
            
        }
    }];
}

#pragma mark - UIKeyBoard Notification Methods
- (void)keyboardWillShow:(NSNotification *)notification
{
    [self changeViewFrameWithKeyBoardNotification:notification isShow:YES];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
    [self changeViewFrameWithKeyBoardNotification:notification isShow:NO];
}

- (void)changeViewFrameWithKeyBoardNotification:(NSNotification *)notification isShow:(BOOL)isShow
{
    NSDictionary *userInfo = [notification userInfo];
    //NSValue *keyFrameValue = userInfo[UIKeyboardFrameEndUserInfoKey];
    //CGRect keyboardFrame = [keyFrameValue CGRectValue];
    
    NSNumber *animationDur = userInfo[UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *animationCur = userInfo[UIKeyboardAnimationCurveUserInfoKey];
    
    [UIView beginAnimations:@"changeViewFrame" context:nil];
    [UIView setAnimationCurve:[animationCur integerValue]];
    [UIView setAnimationDuration:[animationDur doubleValue]];
    [self.view setFrame:CGRectMake(0, isShow?-80:64, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
    [UIView commitAnimations];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.fieldPassWord resignFirstResponder];
    [self.fieldUserName resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
